package com.caisejingyu.whales.control.toast;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;

import com.caisejingyu.whales.common.utils.AppUtil;
import com.caisejingyu.whales.common.utils.ClassUtil;


/**
 * ToastBuilder
 *
 */
@SuppressWarnings({"WeakerAccess", "unused"})
public class ToastUtil implements IToastBuilder {

    private static int sDefaultIconType = ICON_TYPE_ERROR;
    private static String sDefaultNetError = "网络异常，请重试";
    private static Class<? extends IToastBuilder> sToastBuilderProxyClass;

    public static void setToastBuilderProxyClass(Class<? extends IToastBuilder> dialogBuilderProxyClass) {
        sToastBuilderProxyClass = dialogBuilderProxyClass;
    }

    public static void setDefaultIconType(int type) {
        sDefaultIconType = type;
    }

    public static void setDefaultNetError(String content) {
        sDefaultNetError = content;
    }

    public static Class<? extends IToastBuilder> getToastBuilderProxyClass() {
        return sToastBuilderProxyClass;
    }

    private IToastBuilder mToastBuilderProxy;

    public ToastUtil() {
        if (getToastBuilderProxyClass() != null) {
            mToastBuilderProxy = ClassUtil.newInstance(getToastBuilderProxyClass());
        }
    }

    @Override
    public IToastBuilder setIcon(Drawable drawable) {
        mToastBuilderProxy.setIcon(drawable);
        return mToastBuilderProxy;
    }

    @Override
    public IToastBuilder setIconType(int type) {
        mToastBuilderProxy.setIconType(type);
        return mToastBuilderProxy;
    }

    @Override
    public IToastBuilder setContent(CharSequence content) {
        mToastBuilderProxy.setContent(content);
        return mToastBuilderProxy;
    }

    @Override
    public IToastBuilder setDuration(int duration) {
        mToastBuilderProxy.setDuration(duration);
        return mToastBuilderProxy;
    }

    @Override
    public IToastBuilder setGravity(int gravity) {
        mToastBuilderProxy.setGravity(gravity);
        return mToastBuilderProxy;
    }

    @Override
    public IToastBuilder cancelLast(boolean cancelLast) {
        mToastBuilderProxy.cancelLast(cancelLast);
        return mToastBuilderProxy;
    }

    @Override
    public IToastBuilder replace(String toastId, IToastAction replaceAction) {
        mToastBuilderProxy.replace(toastId, replaceAction);
        return mToastBuilderProxy;
    }

    @Override
    public IToastBuilder setView(@LayoutRes int layout, IToastAction initAction) {
        mToastBuilderProxy.setView(layout, initAction);
        return mToastBuilderProxy;
    }

    @Override
    public void show(@NonNull Context context) {
        mToastBuilderProxy.show(context);
    }

    public static void showNetError() {
        show(sDefaultIconType, sDefaultNetError);
    }

    public static void show(CharSequence content) {
        show(sDefaultIconType, content);
    }

    public static void show(int iconType, CharSequence content) {
        show(AppUtil.getContext(), iconType, content);
    }

    public static void show(Context context, int iconType, CharSequence content) {
        new ToastUtil().setIconType(iconType).setContent(content).show(context);
    }
}
